package com.mac.service;

import com.mac.mapper.ArticlesMapper;
import com.mac.mapper.UserMapper;
import com.mac.pojo.Articles;
import com.mac.pojo.PageBean;
import com.mac.pojo.User;
import com.mac.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

@SuppressWarnings({"all"})
public class ArticlesService implements ArticlesMapper {

    // 定义logger对象
    private static final Logger LOGGER = LoggerFactory.getLogger("ArticlesService.Class");

    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public ArrayList<Articles> selectAllArticles() {
        SqlSession sqlSession = factory.openSession();

        ArticlesMapper articlesMapper = sqlSession.getMapper(ArticlesMapper.class);

        ArrayList<Articles> articles;
        try {
            articles = articlesMapper.selectAllArticles();
            LOGGER.info("selectAllArticles() -> success");
            sqlSession.close();
            return articles;
        } catch (Exception e) {
            LOGGER.error("selectAllArticles() -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    @Override
    public ArrayList<Articles> selectAllByAuthorId(int authorId) {

        SqlSession sqlSession = factory.openSession();

        ArticlesMapper articlesMapper = sqlSession.getMapper(ArticlesMapper.class);

        ArrayList<Articles> articles;
        try {
            articles = articlesMapper.selectAllByAuthorId(authorId);
            LOGGER.info("selectAllByAuthorId(authorId) -> success");
            sqlSession.close();
            return articles;
        } catch (Exception e) {
            LOGGER.error("selectAllByAuthorId(authorId) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Articles searchOneByTitle(String title) {
        SqlSession sqlSession = factory.openSession();

        ArticlesMapper articlesMapper = sqlSession.getMapper(ArticlesMapper.class);

        Articles article;

        try {
            article = articlesMapper.searchOneByTitle(title);
            LOGGER.info("searchOneByTitle(title) -> success");
            sqlSession.close();
            return article;
        } catch (Exception e) {
            LOGGER.error("searchOneByTitle(title) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Articles searchOneByArticleId(int articleId) {
        SqlSession sqlSession = factory.openSession();

        ArticlesMapper articlesMapper = sqlSession.getMapper(ArticlesMapper.class);

        Articles article;

        try {
            article = articlesMapper.searchOneByArticleId(articleId);
            LOGGER.info("searchOneByArticleId(articleId) -> success");
            sqlSession.close();
            return article;
        } catch (Exception e) {
            LOGGER.error("searchOneByArticleId(articleId) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    @Override
    public ArrayList<Articles> searchAllByTitle(String title) {
        SqlSession sqlSession = factory.openSession();

        ArticlesMapper articlesMapper = sqlSession.getMapper(ArticlesMapper.class);

        ArrayList<Articles> articles;
        try {
             articles = articlesMapper.searchAllByTitle(title);
            LOGGER.info("searchAllByTitle(title) -> success");
            sqlSession.close();
            return articles;
        } catch (Exception e) {
            LOGGER.error("searchAllByTitle(title) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    @Override
    public ArrayList<Articles> searchAllByStatus(String status) {
        SqlSession sqlSession = factory.openSession();

        ArticlesMapper articlesMapper = sqlSession.getMapper(ArticlesMapper.class);

        ArrayList<Articles> articles;
        try {
            articles = articlesMapper.searchAllByStatus(status);
            LOGGER.info("searchAllByStatus(status) -> success");
            sqlSession.close();
            return articles;
        } catch (Exception e) {
            LOGGER.error("searchAllByStatus(status) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }

    }

    @Override
    public boolean addArticle(Articles article) {
        SqlSession sqlSession = factory.openSession();

        ArticlesMapper articlesMapper = sqlSession.getMapper(ArticlesMapper.class);

        try {
            articlesMapper.addArticle(article);
            sqlSession.commit();
            LOGGER.info("addArticle(article) -> success");
            sqlSession.close();
            return true;
        } catch (Exception e) {
            LOGGER.error("addArticle(article) -> failed",e);
            sqlSession.close();
            return false;
        }
    }

    @Override
    public boolean deleteByTitle(String title) {
        SqlSession sqlSession = factory.openSession();

        ArticlesMapper articlesMapper = sqlSession.getMapper(ArticlesMapper.class);

        try {
            articlesMapper.deleteByTitle(title);
            sqlSession.commit();
            LOGGER.info("deleteByTitle(title) -> success");
            sqlSession.close();
            return true;
        } catch (Exception e) {
            LOGGER.error("deleteByTitle(title) -> failed",e);
            sqlSession.close();
            return false;
        }
    }

    @Override
    public boolean changeArticleInfo(Articles article) {
        SqlSession sqlSession = factory.openSession();

        ArticlesMapper articlesMapper = sqlSession.getMapper(ArticlesMapper.class);

        try {
            articlesMapper.changeArticleInfo(article);
            sqlSession.commit();
            LOGGER.info("changeArticleInfo(article) -> success");
            sqlSession.close();
            return true;
        } catch (Exception e) {
            LOGGER.error("changeArticleInfo(article) -> failed",e);
            sqlSession.close();
            return false;
        }
    }

    public ArrayList<User> selectAll() {
        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<User> users;
        try {
            users = userMapper.selectAll();
            LOGGER.info("userMapper.selectAll() -> succeed");
            sqlSession.close();
            return users;
        } catch (Exception e) {
            LOGGER.error("userMapper.selectAll() -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public boolean deleteByIds(int[] ids) {
        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        try {
            userMapper.deleteByIds(ids);
            sqlSession.commit();
            sqlSession.close();
            LOGGER.info("userMapper.deleteByIds(ids) -> succeed");
            return true;
        } catch (Exception e) {
            LOGGER.error("userMapper.deleteByIds(ids) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public PageBean<User> selectByPage(int currentPage, int pageSize) {

        //4. 计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<User> users;
        try {
            users = userMapper.selectByPage(begin,size);

            //6. 查询总记录数
            int totalCount = userMapper.selectTotalCount();

            //7. 封装PageBean对象
            PageBean<User> pageBean = new PageBean<>();
            pageBean.setRows(users);
            pageBean.setTotalCount(totalCount);

            LOGGER.info("userMapper.selectByPage(begin,size) -> succeed");
            sqlSession.close();
            return pageBean;
        } catch (Exception e) {
            LOGGER.error("userMapper.selectByPage(begin,size) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public Integer selectTotalCount() {
        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Integer total;
        try {
            total = userMapper.selectTotalCount();
            LOGGER.info("userMapper.selectTotalCount() -> succeed");
            sqlSession.close();
            return total;
        } catch (Exception e) {
            LOGGER.error("userMapper.selectTotalCount() -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    @Override
    public ArrayList<Articles> selectByPageAndCondition(int begin, int size, Articles article) {
        return null;
    }

    @Override
    public Integer selectTotalCountByCondition(Articles article) {
        return 0;
    }

    public PageBean<User> selectByPageAndCondition(int currentPage, int pageSize, User user) {

        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<User> users;
        try {
            users = userMapper.selectByPageAndCondition(begin,size,user);


            //6. 查询总记录数
            int totalCount = userMapper.selectTotalCountByCondition(user);

            //7. 封装PageBean对象
            PageBean<User> pageBean = new PageBean<>();
            pageBean.setRows(users);
            pageBean.setTotalCount(totalCount);

            LOGGER.info("selectByPageAndCondition(begin,size,user) -> succeed");
            sqlSession.close();
            return pageBean;
        } catch (Exception e) {
            LOGGER.error("selectByPageAndCondition(begin,size,user) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }

    public Integer selectTotalCountByCondition(User user) {
        SqlSession sqlSession = factory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Integer total;
        try {
            total = userMapper.selectTotalCountByCondition(user);
            LOGGER.info("userMapper.selectTotalCountByCondition(user) -> succeed");
            sqlSession.close();
            return total;
        } catch (Exception e) {
            LOGGER.error("userMapper.selectTotalCountByCondition(user) -> failed",e);
            sqlSession.close();
            throw new RuntimeException(e);
        }
    }
}
